package org.cttic.visualization.visualization.z_service.six;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.cttic.visualization.visualization.common.module.serviceCountData;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.cttic.visualization.visualization.util.sixUtil.interactiveData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.cttic.visualization.visualization.util.sixUtil.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-27 16:16
 * 2.6.3路网接口使用频率监控
 */
@Service
public class interactiveCountServiceImpl implements interactiveCountService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private interactiveQueryUtil interactiveQueryUtil;
    @Override
    public String interactiveCount(String inputData) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(inputData);
        String provinceId = jsonData.getString("provinceId");
        String roadSectionId = jsonData.getString("roadSectionId");
        String queryType = jsonData.getString("queryType");
        String queryValue1 = jsonData.getString("queryValue1");
        interactiveData interactiveData = new interactiveData();
        List<interactiveData.Interface> list = new ArrayList<>();
        //todo 时间段查询
        if(queryType.equals("1")){
            interactiveData.Interface anInterface = new interactiveData.Interface();
            interactiveData.Interface anInterface2 = new interactiveData.Interface();
            String queryValue2 = jsonData.getString("queryValue2");
            String startTime = DateUtil.TtoTime(queryValue1);
            String endTime = DateUtil.TtoTime(queryValue2);
            System.out.println("***************使用频率监控查询起始时间："+startTime+"结束时间："+endTime+"****************");
            long access = interactiveQueryUtil.queryCountMultiple_access(provinceId, startTime, endTime);
            long info = interactiveQueryUtil.queryCountMultiple_info(provinceId, startTime, endTime);

                //北斗调用路网
                anInterface.setInvokeType("1");
                anInterface.setInvokeNums(access+"");
                //路网调用北斗
                anInterface2.setInvokeType("2");
                anInterface2.setInvokeNums(info+"");
                System.out.println("***************使用频率监控查询结束****************");
                list.add(anInterface);
                list.add(anInterface2);
        }else if(queryType.equals("0")){

        }

//        if(provinceId==null){
//            //按时间段查询
//            if(queryType.equals("1")){
//            String queryValue2 = jsonData.getString("queryValue2");
//            String startTime = DateUtil.TtoTime(queryValue1);
//            String endTime = DateUtil.TtoTime(queryValue2);
//            String querySql1="SELECT sum(invokeNums) as invokeNums,processTime  FROM `interactiveCount`  WHERE invokeType= '"+ 1 + "' " +
//                    "and processTime<"+endTime+" and  processTime>"+startTime;
//            System.out.println(querySql1);
//                Map<String, Object> stringObjectMap1 = jdbcTemplate.queryForMap(querySql1);
//                if(stringObjectMap1!=null&&stringObjectMap1.size()>0){
//                    interactiveData.Interface anInterface = new interactiveData.Interface();
//                    cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(stringObjectMap1);
//                    Object invokeNums = jsonObject.get("invokeNums");
//                    anInterface.setInvokeType("1");
//                    if(invokeNums!=null){
//                        anInterface.setInvokeNums(String.valueOf(invokeNums));
//                    list.add(anInterface);
//                }
//            }
//            String querySql2="SELECT sum(invokeNums) as invokeNums,processTime  FROM `interactiveCount`  WHERE invokeType= '"+ 2 + "' " +
//                    "and processTime<"+endTime+" and  processTime>"+startTime;
//            System.out.println(querySql2);
//            Map<String, Object> stringObjectMap2 = jdbcTemplate.queryForMap(querySql2);
//                if(stringObjectMap2!=null&&stringObjectMap2.size()>0){
//                    interactiveData.Interface anInterface2 = new interactiveData.Interface();
//                    cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(stringObjectMap2);
//                    Object invokeNums = jsonObject.get("invokeNums");
//                    anInterface2.setInvokeType("2");
//                    if(invokeNums!=null){
//                        anInterface2.setInvokeNums(String.valueOf(invokeNums));
//                    }
//                    list.add(anInterface2);
//                }
//            }
//
//            //todo 单省
//        }else if(!provinceId.contains("|")){
//            //按时间段查询
//            if(queryType.equals("1")){
//                String queryValue2 = jsonData.getString("queryValue2");
//                String startTime = DateUtil.TtoTime(queryValue1);
//                String endTime = DateUtil.TtoTime(queryValue2);
//                interactiveData.Interface anInterface = new interactiveData.Interface();
//                interactiveData.Interface anInterface2 = new interactiveData.Interface();
//                System.out.println("***************单省查询起始时间："+startTime+"结束时间："+endTime+"****************");
//                long count_access = interactiveQueryUtil.queryCount_access(provinceId, startTime, endTime);
//                long count_info = interactiveQueryUtil.queryCount_info(provinceId, startTime, endTime);
//                //北斗调用路网
//                anInterface.setInvokeType("1");
//                anInterface.setInvokeNums(count_access+"");
//                //路网调用北斗
//                anInterface2.setInvokeType("2");
//                anInterface2.setInvokeNums(count_info+"");
//                System.out.println("***************单省查询结束****************");
//                list.add(anInterface);
//                list.add(anInterface2);
//                //按时间点查询
//            }else if (queryType.equals("0")){
//
//            }
//            //todo 多省
//        }else if(provinceId.contains("|")){
//            //按时间段查询
//            if(queryType.equals("1")){
//                String queryValue2 = jsonData.getString("queryValue2");
//                String startTime = DateUtil.TtoTime(queryValue1);
//                String endTime = DateUtil.TtoTime(queryValue2);
//                interactiveData.Interface anInterface = new interactiveData.Interface();
//                interactiveData.Interface anInterface2 = new interactiveData.Interface();
//                System.out.println("***************多省查询起始时间："+startTime+"结束时间："+endTime+"****************");
//                long count_access = interactiveQueryUtil.queryCountMultiple_access(provinceId, startTime, endTime);
//                long count_info = interactiveQueryUtil.queryCountMultiple_info(provinceId, startTime, endTime);
//                //北斗调用路网
//                anInterface.setInvokeType("1");
//                anInterface.setInvokeNums(count_access+"");
//                //路网调用北斗
//                anInterface2.setInvokeType("2");
//                anInterface2.setInvokeNums(count_info+"");
//                System.out.println("***************多省查询结束****************");
//                list.add(anInterface);
//                list.add(anInterface2);
//            }else if(queryType.equals("0")){
//
//            }
//        }
//
        interactiveData.setInterList(list);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String value = mapper.writeValueAsString(interactiveData);
            return value;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
